﻿namespace NovellSecurity.Caching
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Web.Security;
  using Sitecore.Diagnostics;

  public class ClearCacheAgent
  {
    protected string RoleProviderName { get; set; }
    protected string MembershipProviderName { get; set; }

    /// <summary>
    /// Initializes a new instance of the <see cref="ClearCacheAgent"/> class.
    /// </summary>
    /// <param name="roleProviderName">Name of the role provider.</param>
    /// <param name="membershipProviderName">Name of the membership provider.</param>
    public ClearCacheAgent(string roleProviderName, string membershipProviderName)
    {
      this.RoleProviderName = roleProviderName;
      this.MembershipProviderName = membershipProviderName;
    }

    /// <summary>
    /// Runs this instance.
    /// </summary>
    public void Run()
    {
      NovellSecurity.MembershipProvider membershipProvider = (NovellSecurity.MembershipProvider)Membership.Providers[this.MembershipProviderName];
      membershipProvider.FillUsersCacheFromEdir();
      NovellSecurity.RoleProvider roleProvider = (NovellSecurity.RoleProvider)Roles.Providers[this.RoleProviderName];
      roleProvider.GetRolesFromEdirAndFillCache();
    }
  }
}
